package helexis.game.tyrian.screen;

import helexis.game.tyrian.Tyrian;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

public abstract class AbstractScreen implements Screen {
	
	
    // the fixed viewport dimensions (ratio: 1.6)
    public static final int GAME_VIEWPORT_WIDTH = 400, GAME_VIEWPORT_HEIGHT = 240;
    public static final int MENU_VIEWPORT_WIDTH = 800, MENU_VIEWPORT_HEIGHT = 480;
	
	protected final Tyrian game;
	protected final Stage stage;
	
	protected final BitmapFont font;
	protected final SpriteBatch batch;
	protected final Skin skin;
	protected final TextureAtlas atlas;
	
	
	protected Table table;
	
	
	
	public AbstractScreen(Tyrian game) {
		this.game = game;
		
		this.font = new BitmapFont();
		
		this.batch = new SpriteBatch();
		
		/*
		 * 스킨에서 사용할 파일을 로딩합니다.
		 * 
		 * 스킨을 사용하기 위해서는
		 * tyrian-game-android 프로젝트의 assets 디렉터리 이하에
		 * 아래와 같은 놈들이 꼭!!! 있어야 합니다.
		 * 
		 * tyrian-game-android
		 *   └ assets
		 *        └ skin/
		 *             ├ default.fnt
		 *             ├ default.png
		 *             ├ uiskin.atlas
		 *             ├ uiskin.json
		 *             └ uiskin.png
		 * 
		 */
		FileHandle skinFile = Gdx.files.internal("skin/uiskin.json");
		this.skin = new Skin(skinFile);
		

//		// 요건 예전 버전....
//		this.skin = new Skin(Gdx.files.internal("uiskin.json"), Gdx.files.internal("uiskin.png"));
//
		
		this.atlas = new TextureAtlas(Gdx.files.internal("image-atlases/pages.atlas"));
		
		
		/*
		 * Tutorial 3.
		 * 
		 * Stage 생성...
		 * 
		 * Tutorial 9.
		 * 
		 * Stage 에 width / height 추가
		 */
        int width = isGameScreen() ? GAME_VIEWPORT_WIDTH : MENU_VIEWPORT_WIDTH;
        int height = isGameScreen() ? GAME_VIEWPORT_HEIGHT : MENU_VIEWPORT_HEIGHT;
        this.stage = new Stage(width, height, true);
	}
	
	
	protected String getName() {
        return getClass().getSimpleName();
    }
	
	
	
	protected boolean isGameScreen() {
		return false;
	}//end of isGameScreen()
	
	

	@Override
	public void render(float delta) {
		
		/*
		 * Tutorial 2.
		 * 
		 * RGB 검은색을 지정하여 화면을 클리어하는 코드
		 */
		Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		/*
		 * Tutorial 3.
		 * 
		 * 스테이지를 업데이트하고 스테이지 내 액터들을 그린다...
		 */
		this.stage.act(delta);
		
		this.stage.draw();

	}

	@Override
	public void resize(int width, int height) {
		
		Gdx.app.log(Tyrian.LOG, "Resizing screen : " + getName() + " to : " + width + " x " + height);
		
		/*
		 * Tutorial 9.
		 * 
		 * 아래의 ViewPort 설정 제거...
		 */
//		this.stage.setViewport(width, height, true);

	}
	
	
	public Skin getSkin() {
		return this.skin;
	}//end of getSkin()
	

	@Override
	public void show() {
		Gdx.app.log(Tyrian.LOG, "Showing screen... : " + getName());
		
		/*
		 * Tutorial 4.
		 * 
		 * input processor 설정
		 */
		Gdx.input.setInputProcessor(this.stage);
	}

	@Override
	public void hide() {
		Gdx.app.log(Tyrian.LOG, "Hiding screen... : " + getName());
	}

	@Override
	public void pause() {
		Gdx.app.log(Tyrian.LOG, "Pausing screen... : " + getName());
	}

	@Override
	public void resume() {
		Gdx.app.log(Tyrian.LOG, "Resuming screen... : " + getName());
	}

	@Override
	public void dispose() {
		
		Gdx.app.log(Tyrian.LOG, "Disposing screen... : " + getName());
		
		/*
		 * Tutorial 3.
		 * 
		 * 사용한 스테이지는 반환 작업 수행...
		 */
		this.stage.dispose();
		this.batch.dispose();
		this.font.dispose();
		
		this.atlas.dispose();

	}//end of dispose()

}
//end of AbstractScreen.java